#ifndef THINK_THINK_VIEW_DRIVER_THINK_H
#define THINK_THINK_VIEW_DRIVER_THINK_H

static char* think_view_driver_think =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\view\\driver;\n"
	"\n"
	"use think\\App;\n"
	"use think\\exception\\TemplateNotFoundException;\n"
	"use think\\Loader;\n"
	"use think\\Log;\n"
	"use think\\Request;\n"
	"use think\\Template;\n"
	"\n"
	"class Think\n"
	"{\n"
	"    // 模板引擎实例\n"
	"    private $template;\n"
	"    // 模板引擎参数\n"
	"    protected $config = [\n"
	"        // 视图基础目录（集中式）\n"
	"        \'view_base\'   => \'\',\n"
	"        // 模板起始路径\n"
	"        \'view_path\'   => \'\',\n"
	"        // 模板文件后缀\n"
	"        \'view_suffix\' => \'html\',\n"
	"        // 模板文件名分隔符\n"
	"        \'view_depr\'   => DS,\n"
	"        // 是否开启模板编译缓存,设为false则每次都会重新编译\n"
	"        \'tpl_cache\'   => true,\n"
	"        // 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写\n"
	"        \'auto_rule\'   => 1,\n"
	"    ];\n"
	"\n"
	"    public function __construct($config = [])\n"
	"    {\n"
	"        $this->config = array_merge($this->config, $config);\n"
	"        if (empty($this->config[\'view_path\'])) {\n"
	"            $this->config[\'view_path\'] = App::$modulePath . \'view\' . DS;\n"
	"        }\n"
	"\n"
	"        $this->template = new Template($this->config);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 检测是否存在模板文件\n"
	"     * @access public\n"
	"     * @param string $template 模板文件或者模板规则\n"
	"     * @return bool\n"
	"     */\n"
	"    public function exists($template)\n"
	"    {\n"
	"        if (\'\' == pathinfo($template, PATHINFO_EXTENSION)) {\n"
	"            // 获取模板文件名\n"
	"            $template = $this->parseTemplate($template);\n"
	"        }\n"
	"        return is_file($template);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 渲染模板文件\n"
	"     * @access public\n"
	"     * @param string    $template 模板文件\n"
	"     * @param array     $data 模板变量\n"
	"     * @param array     $config 模板参数\n"
	"     * @return void\n"
	"     */\n"
	"    public function fetch($template, $data = [], $config = [])\n"
	"    {\n"
	"        if (\'\' == pathinfo($template, PATHINFO_EXTENSION)) {\n"
	"            // 获取模板文件名\n"
	"            $template = $this->parseTemplate($template);\n"
	"        }\n"
	"        // 模板不存在 抛出异常\n"
	"        if (!is_file($template)) {\n"
	"            throw new TemplateNotFoundException(\'template not exists:\' . $template, $template);\n"
	"        }\n"
	"        // 记录视图信息\n"
	"        App::$debug && Log::record(\'[ VIEW ] \' . $template . \' [ \' . var_export(array_keys($data), true) . \' ]\', \'info\');\n"
	"        $this->template->fetch($template, $data, $config);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 渲染模板内容\n"
	"     * @access public\n"
	"     * @param string    $template 模板内容\n"
	"     * @param array     $data 模板变量\n"
	"     * @param array     $config 模板参数\n"
	"     * @return void\n"
	"     */\n"
	"    public function display($template, $data = [], $config = [])\n"
	"    {\n"
	"        $this->template->display($template, $data, $config);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 自动定位模板文件\n"
	"     * @access private\n"
	"     * @param string $template 模板文件规则\n"
	"     * @return string\n"
	"     */\n"
	"    private function parseTemplate($template)\n"
	"    {\n"
	"        // 分析模板文件规则\n"
	"        $request = Request::instance();\n"
	"        // 获取视图根目录\n"
	"        if (strpos($template, \'@\')) {\n"
	"            // 跨模块调用\n"
	"            list($module, $template) = explode(\'@\', $template);\n"
	"        }\n"
	"        if ($this->config[\'view_base\']) {\n"
	"            // 基础视图目录\n"
	"            $module = isset($module) ? $module : $request->module();\n"
	"            $path   = $this->config[\'view_base\'] . ($module ? $module . DS : \'\');\n"
	"        } else {\n"
	"            $path = isset($module) ? APP_PATH . $module . DS . \'view\' . DS : $this->config[\'view_path\'];\n"
	"        }\n"
	"\n"
	"        $depr = $this->config[\'view_depr\'];\n"
	"        if (0 !== strpos($template, \'/\')) {\n"
	"            $template   = str_replace([\'/\', \':\'], $depr, $template);\n"
	"            $controller = Loader::parseName($request->controller());\n"
	"            if ($controller) {\n"
	"                if (\'\' == $template) {\n"
	"                    // 如果模板文件名为空 按照默认规则定位\n"
	"                    $template = str_replace(\'.\', DS, $controller) . $depr . (1 == $this->config[\'auto_rule\'] ? Loader::parseName($request->action(true)) : $request->action());\n"
	"                } elseif (false === strpos($template, $depr)) {\n"
	"                    $template = str_replace(\'.\', DS, $controller) . $depr . $template;\n"
	"                }\n"
	"            }\n"
	"        } else {\n"
	"            $template = str_replace([\'/\', \':\'], $depr, substr($template, 1));\n"
	"        }\n"
	"        return $path . ltrim($template, \'/\') . \'.\' . ltrim($this->config[\'view_suffix\'], \'.\');\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 配置或者获取模板引擎参数\n"
	"     * @access private\n"
	"     * @param string|array  $name 参数名\n"
	"     * @param mixed         $value 参数值\n"
	"     * @return mixed\n"
	"     */\n"
	"    public function config($name, $value = null)\n"
	"    {\n"
	"        if (is_array($name)) {\n"
	"            $this->template->config($name);\n"
	"            $this->config = array_merge($this->config, $name);\n"
	"        } elseif (is_null($value)) {\n"
	"            return $this->template->config($name);\n"
	"        } else {\n"
	"            $this->template->$name = $value;\n"
	"            $this->config[$name]   = $value;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    public function __call($method, $params)\n"
	"    {\n"
	"        return call_user_func_array([$this->template, $method], $params);\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif